/**
 * BooleanFormatter
 *
 * @author Chris Pratt
 *
 * 11/26/2009
 */
package com.anodyzed.onyx.text;

import com.anodyzed.onyx.util.Convert;

public class BooleanFormatter extends TextFormatter {
  private String positive;
  private String negative;

  /**
   * Constructor
   *  
   * @param parent The Parent TextFormat 
   * @param key The Argument Index or Map Key for this node 
   * @param bean The Additional Bean reference String 
   * @param style The Format String 
   */
  public BooleanFormatter (TextFormat parent,CharSequence key,CharSequence bean,CharSequence style) {
    super(parent,key,bean);
    String fmt = style.toString();
    int pos = fmt.indexOf('|');
    if(pos != -1) {
      positive = fmt.substring(0,pos);
      negative = fmt.substring(pos + 1);
    }
  } //BooleanFormatter

  /**
   * Constructor
   *  
   * @param parent The parent TextFormat 
   * @param key The Argument Index or Map Key for this node 
   * @param bean The Additional Bean reference String 
   */
  public BooleanFormatter (TextFormat parent,CharSequence key,CharSequence bean) {
    super(parent,key,bean);
    positive = "true";
    negative = "false";
  } //BooleanFormatter

  /**
   * Return a string representation of a boolean
   *
   * @param args The Formatting Arguments
   * @return The Formatted String
   */
  @Override
  public String toString (Object... args) {
    Object o = getValue(args);
    if(o instanceof Boolean) {
      return ((Boolean)o) ? positive : negative;
    } else if(o instanceof String) {
      return (Convert.toBoolean((String)o)) ? positive : negative;
    }
    return null;
  } //toString

} //*BooleanFormatter
